<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="javax.servlet.*" %>

<%@ page import="java.sql.*" %>

<%
String driverName="com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver").newInstance();

int rowCount = 0;
String url="jdbc:mysql://127.0.0.1/exe";
String user="root";
String password="admin";

ResultSet rs;
ResultSet rsr;
ResultSet rcount;
String rowid = null;
String row = null;
String row2 = null;
String result;

String test="caonimei";

String userid = request.getParameter("userid");
int page1=Integer.parseInt(request.getParameter("page1"));
String page2=request.getParameter("page2");

   Connection conn=DriverManager.getConnection(url,user,password);
   Connection connr=DriverManager.getConnection(url,user,password);
   Connection conncount=DriverManager.getConnection(url,user,password);
   Statement stmt=conn.createStatement();
   Statement stmtr=connr.createStatement();
   Statement stmtcount=conncount.createStatement();
   String SQLI="INSERT INTO comments values('"+userid+"','"+page1+"','"+page2+"');";
   String SQLIU="UPDATE comments SET DATA = '"+page1+"',DATA2 = '"+page2+"' WHERE ID = '"+userid+"';";
   String SQLIS="select * from comments where id='"+userid+"'";
   
   rs=stmt.executeQuery(SQLIS);
   if(rs.next()) 
   { 
   	row = rs.getString(2); 
    row2 = rs.getString(3);
   } 

   
   if(row==null){
   stmt.addBatch(SQLI);
   stmt.executeBatch();
     }
   else{
	   stmt.addBatch(SQLIU);
	   stmt.executeBatch();
     }
   conn.close();
   rs.close();
   stmt.close();
   
   
   rcount = stmtcount.executeQuery("select * from comments");
   while(rcount.next()) { rowCount++; }
  	System.out.println("qiguai"+rowCount);
   
   rsr=stmtr.executeQuery("SELECT * FROM comments ORDER BY DATA DESC");

   int i=0;
   while(rsr.next()&&i <rowCount) 
   { i++;

	rowid=rsr.getString(1);
   	System.out.print(rowid+" ");
   	row = rsr.getString(2); 
   	System.out.print(row+" ");
    row2 = rsr.getString(3);
   	System.out.println(row2);

   } 
   
   
   stmt.close();
   response.setContentType("application/octet-stream"); 
	 OutputStream os = response.getOutputStream();
	 DataOutputStream dos = new DataOutputStream(os);
	 dos.writeUTF(test);
  
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<br><p3>Result</p3>
</head>
<body>
Data:<br>
ID:<%=userid %>  Selection: <%=page1 %>  and <%=page2 %> <br>
saved successfully.<br>

<input type="button" value="Back" onclick="window.location.href='index.jsp'">


</body>
</html>